﻿#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include "HashTable.hpp"

namespace sk
{
	template<class K, class V, class HashFunc = Hash<K>>
	class unordered_map
	{
		struct MapKeyOfT
		{
			const K& operator()(const pair<K, V>& kv)
			{
				return kv.first;
			}
		};
	public:
		typedef typename LinkHash::HashTable<K, pair<K, V>, MapKeyOfT, HashFunc>::iterator iterator;

		iterator begin()
		{
			return _ht.begin();
		}
		iterator end()
		{
			return _ht.end();
		}
		pair<iterator, bool> insert(const pair<K, V>& kv)
		{
			return _ht.insert(kv);
		}

		V& operator[](const K& key)
		{
			auto ret = _ht.insert(make_pair(key, V())).first;
			return ret->second;
		}
	private:
		LinkHash::HashTable<K, pair<K, V>, MapKeyOfT, HashFunc> _ht;
	};

	void test_unordered_map()
	{
		unordered_map<string, int> um;
		um.insert(make_pair("sdufd", 1));
		um.insert(make_pair("sdsfd", 1));
		um.insert(make_pair("sadfd", 1));
		um.insert(make_pair("sdffd", 1));
		um.insert(make_pair("sdwfd", 1));
		string s = "sxcvwd";
		um["sxcvwd"] = 2;
		um["sxcvwd"]++;
		um["sxcvwd"]++;
		um["sxcvwd"]++;

		unordered_map<string, int> dict1(um);
		unordered_map<string, int> dict2 = dict1;

		unordered_map<string, int>::iterator it = um.begin();

		for (auto& e : dict2)
		{
			cout << e.first << ":" << e.second << endl;
		}
	}
}